package com.youlai.boot.form.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.boot.core.web.PageResult;
import com.youlai.boot.core.web.Result;
import com.youlai.boot.form.model.form.FormCollectRecordForm;
import com.youlai.boot.form.model.form.FormCollectTaskForm;
import com.youlai.boot.form.model.query.FormCollectTargetQuery;
import com.youlai.boot.form.model.vo.FormCollectTargetVO;
import com.youlai.boot.form.service.FormCollectRecordService;
import com.youlai.boot.form.service.FormCollectTargetService;
import com.youlai.boot.form.service.FormCollectTaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 表单收集接口
 *
 * @author fwj
 * @since 2025/11/7
 */
@Tag(name = "表单收集接口")
@RestController
@RequestMapping("/api/v1/collect")
@RequiredArgsConstructor
public class FormCollectController {

    private final FormCollectTaskService formCollectTaskService;
    private final FormCollectTargetService formCollectTargetService;
    private final FormCollectRecordService formCollectRecordService;

    @Operation(summary = "创建表单收集任务")
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPerm('form:collect:add')")
    public Result<?> createTask(@RequestBody @Valid FormCollectTaskForm collectTaskForm) {
        return Result.success(formCollectTaskService.save(collectTaskForm));
    }

    @Operation(summary = "删除表单收集任务")
    @PostMapping("/delete/{id}")
    @PreAuthorize("@ss.hasPerm('form:collect:delete')")
    public Result<?> deleteTask(@Parameter(description = "任务id") @PathVariable Long id) {
        return Result.success(formCollectTaskService.delete(id));
    }

    @Operation(summary = "更新表单收集任务")
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPerm('form:collect:edit')")
    public Result<?> updateTask(@RequestBody @Valid FormCollectTaskForm collectTaskForm) {
        return Result.success(formCollectTaskService.edit(collectTaskForm));
    }

    @Operation(summary = "分页查询表单收集任务列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('form:collect:list')")
    public PageResult<FormCollectTargetQuery> getTaskPage( FormCollectTargetQuery queryParams ){
        Page<FormCollectTargetQuery> result = formCollectTargetService.getTaskPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "发布表单收集任务")
    @PostMapping("publish/{id}")
    @PreAuthorize("@ss.hasPerm('form:collect:publish')")
    public Result<?> publishTask(@Parameter(description = "任务id") @PathVariable Long id) {
        return Result.success(formCollectTaskService.publishTask(id));
    }

    @Operation(summary = "用户填写表单任务")
    @PostMapping("/fill")
    @PreAuthorize("@ss.hasPerm('form:collect:fill')")
    public Result<?> fillTask(@RequestBody @Valid FormCollectRecordForm collectRecordForm) {
        return Result.success(formCollectRecordService.fillTask(collectRecordForm));
    }

    @Operation(summary = "用户提交表单任务")
    @PostMapping("submit/{id}")
    @PreAuthorize("@ss.hasPerm('form:collect:submit')")
    public Result<?> submitTask(@Parameter(description = "任务id") @PathVariable Long id) {
        return Result.success(formCollectRecordService.submitTask(id));
    }


}
